package com.lixiaoyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixiaoyao.reggie.entity.Employee;
import com.lixiaoyao.reggie.entity.R;
import com.lixiaoyao.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 1.将收到的明文密码md5加密转为密文
     * 2.通过用户名查找数据库
     * 3.将查找到的记录的密码与密文做比较
     * 4.查看员工状态是否禁用
     * 5.将id寸到session里
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        if(emp==null){
           return R.error("登录失败");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }
        if(emp.getStatus()==0){
            return R.error("账号被冻结");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){

        Long  cid= (Long) request.getSession().getAttribute("employee");
        String asHex = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(asHex);

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(cid);
//        employee.setUpdateUser(cid);
        boolean save = employeeService.save(employee);
        log.info("{}",save);

        return R.success("保存成功");
    }

    /**
     * page?page=1&pageSize=10&name=123
     * 分页查询控制器
     */
    @GetMapping("/page")
    public R<Page> getPage(@RequestParam("page") int pages,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}",pages,pageSize,name);

        Page<Employee> page = new Page<>(pages,pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(page,queryWrapper);
        log.info("分页数据：{}",page);
        return R.success(page);
    }

    /**
     * 修改员工启禁用状态
     *
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee,HttpServletRequest request){
        Long empid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empid);
        log.info(employee.toString());
        long id = Thread.currentThread().getId();
        log.info("controller当前线程id为：{}",id);
        employeeService.updateById(employee);
        return R.success("修改员工信息成功");
    }
    /**
     * 根据id查找数据
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee==null){
            return R.error("查找不到员工信息");
        }
        return R.success(employee);
    }
}
